import plugin from '../../../lib/plugins/plugin.js';
import fetch from 'node-fetch';

// 假设的JSON数据，实际使用时可以从文件或数据库中读取
const pokemonGuides = {
  "急冻鸟": [
    {
      "类型": "秒人流",
      "技能": ["心之眼", "绝对零度", "替身", "冰冻光束/暴风"],
      "努力值": {"HP": 252, "速度": 252}
    },
    {
      "类型": "消耗流",
      "技能": ["剧毒", "羽栖", "替身", "冰冻光束"],
      "努力值": {"HP": 252, "特防": 252}
    }
  ],
  "闪电鸟": {
    "类型": "进攻型",
    "技能": ["十万伏特", "觉醒力量冰", "热风", "急速折返/羽栖息"],
    "努力值": {"特攻": 252, "速度": 252}
  },
  "火焰鸟": {
    "类型": "进攻型",
    "技能": ["燃尽", "暴风", "近身打", "觉冰/觉地"],
    "努力值": {"特攻": 252, "速度": 252}
  },
  "雷公": {
    "努力值": {"速度": 252, "特攻": 252},
    "性格": "胆小",
    "个体值": ["速度", "特攻", "生命"],
    "道具": "讲究眼睛",
    "技能": ["伏特替换", "波导弹", "阴影球", "神通力"]
  },
  "炎帝": {
    "努力值": {"速度": 252, "攻击": 252},
    "性格": "爽朗",
    "个体值": ["速度", "攻击", "生命"],
    "道具": "讲究围巾",
    "技能": ["圣火", "神速", "重踏", "尖石攻击"]
  },
  "水君": {
    "努力值": {"物防": 252, "生命": 252},
    "性格": "加物防减攻击",
    "个体值": ["生命", "物防", "特防"],
    "道具": "剩饭",
    "技能": ["热水", "剧毒", "保护", "冰冻光束"]
  },
  "Y梦": {
    "个体值": ["特攻", "速度", "生命"],
    "努力值": {"特攻": 252, "速度": 252},
    "性格": "胆小",
    "技能": ["精神击破", "冰冻光束", "喷色火焰", "暗影球/真气弹", "冥想（可选替换为攻击技能）"]
  },
  "X梦": {
    "个体值": ["速度", "攻击", "生命"],
    "性格": ["固执", "爽朗"],
    "努力值": {"速度": 252, "攻击": 252},
    "技能": ["健美", "踢到", "冰冻拳", "尖石攻击/岩崩", "其他进攻技能（具体未指定）"]
  },
  "普通超梦": {
    "个体值": ["特攻", "速度", "生命"],
    "努力值": {"特攻": 252, "速度": 252},
    "性格": "胆小",
    "道具": "讲究眼睛",
    "技能": ["精神击破", "冰冻光束", "喷色火焰", "暗影球/真气弹", "冥想（可选替换为攻击技能）"]
  }
};

export class PokemonGuideQuery extends plugin {
  constructor() {
    super({
      name: '宝可梦攻略查询',
      dsc: '查询指定宝可梦的攻略信息',
      event: 'message',
      priority: 5000,
      rule: [
        {
          reg: `^#(宝可梦攻略)(.*)`,
          fnc: 'pokemonGuideQuery'
        },
        {
          reg: `^#攻略列表$`,
          fnc: 'listPokemonGuides'
        },
      ]
    });
  }

  async pokemonGuideQuery(e) {
    let msg = e.msg;
    let pokemonName = msg.replace(/#|宝可梦攻略/g, "").trim();
    let guide = pokemonGuides[pokemonName];

    if (!guide) {
      return await this.reply(`没有找到关于“${pokemonName}”的攻略信息。`);
    }

    let guideStr = this.formatGuide(guide, pokemonName);
    await this.reply(`${guideStr}`);
  }

  async listPokemonGuides(e) {
    let guideNames = Object.keys(pokemonGuides);
    let replyStr = `可查询的宝可梦攻略有：\n${guideNames.join(', ')}`;
    await this.reply(replyStr);
  }

  formatGuide(guide, pokemonName) {
    let guideStr = `宝可梦名称：${pokemonName}\n`;
    let additionalInfo = '';

    if (Array.isArray(guide)) {
      guide.forEach(g => {
        let singleGuideStr = `类型：${g.类型}\n技能：${g.技能.join('、')}\n`;
        if (g.努力值) {
          singleGuideStr += `努力值：`;
          for (let stat in g.努力值) {
            singleGuideStr += `${stat}${g.努力值[stat]}、`;
          }
          singleGuideStr = singleGuideStr.slice(0, -1); // 移除最后一个逗号
          singleGuideStr += '\n';
        }
        if (g.性格) {
          singleGuideStr += `性格：${g.性格}\n`;
        }
        if (g.道具) {
          singleGuideStr += `携带道具：${g.道具}\n`;
        }
        if (g.个体值) {
          singleGuideStr += `推荐个体值：${g.个体值.join('、')}\n`;
        }
        additionalInfo += singleGuideStr + '\n\n';
      });
    } else {
      let singleGuideStr = `类型：${guide.类型}\n技能：${guide.技能.join('、')}\n`;
      if (guide.努力值) {
        singleGuideStr += `努力值：`;
        for (let stat in guide.努力值) {
          singleGuideStr += `${stat}${guide.努力值[stat]}、`;
        }
        singleGuideStr = singleGuideStr.slice(0, -1); // 移除最后一个逗号
        singleGuideStr += '\n';
      }
      if (guide.性格) {
        singleGuideStr += `性格：${guide.性格}\n`;
      }
      if (guide.道具) {
        singleGuideStr += `携带道具：${guide.道具}\n`;
      }
      if (guide.个体值) {
        singleGuideStr += `推荐个体值：${guide.个体值.join('、')}\n`;
      }
      additionalInfo = singleGuideStr;
    }

    // Trim the last newline characters
    return guideStr + additionalInfo.replace(/\s+$/, '');
  }
}